package com.magist.core.util;

import org.apache.commons.codec.digest.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class TxtUtil {
	private static final String HEXES = "0123456789ABCDEF";
	private static final String IP_PARTNER = "(((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))";

	public TxtUtil() {
	}

	public static byte toByte(char c) {
		/* 26 */ byte b = (byte) "0123456789ABCDEF".indexOf(c);
		/* 27 */ return b;
	}

	public static List<Object> unionArray(Boolean repeat, Object[]... arrays) {
		/* 37 */ List<Object> result = new ArrayList();
		try {
			/* 39 */ for (Object[] arr : arrays) {
				/* 40 */ for (Object ar : arr) {
					/* 41 */ if (repeat.booleanValue()) {
						/* 42 */ if (!result.contains(ar)) {
							/* 43 */ result.add(ar);
						}
					} else {
						/* 46 */ result.add(ar);
					}
				}
			}
		} catch (Exception e) {
			/* 51 */ return null;
		}
		/* 53 */ return result;
	}

	public static String toHex(byte[] raw) {
		/* 63 */ if (raw == null) {
			/* 64 */ return null;
		}
		/* 66 */ StringBuilder hex = new StringBuilder(2 * raw.length);
		/* 67 */ byte[] arrayOfByte = raw;
		int j = raw.length;
		for (int i = 0; i < j; i++) {
			byte b = arrayOfByte[i];
			/* 68 */ hex.append("0123456789ABCDEF".charAt((b & 0xF0) >> 4)).append("0123456789ABCDEF".charAt(b & 0xF));
		}
		/* 70 */ return hex.toString();
	}

	public static int toInt(String value) {
		/* 82 */ int result = 0;
		try {
			/* 84 */ result = Integer.valueOf(value).intValue();
		} catch (Exception localException) {
		}

		/* 88 */ return result;
	}

	public static byte[] toBytes(String hex) {
		/* 98 */ int len = hex.length() / 2;
		/* 99 */ hex = hex.toUpperCase();
		/* 100 */ byte[] result = new byte[len];
		/* 101 */ char[] achar = hex.toCharArray();
		/* 102 */ for (int i = 0; i < len; i++) {
			/* 103 */ int pos = i * 2;
			/* 104 */ result[i] = ((byte) (toByte(achar[pos]) << 4 | toByte(achar[(pos + 1)])));
		}
		/* 106 */ return result;
	}

	public static String toUnicode(String value) {
		/* 116 */ String result = "";
		/* 117 */ String temp = "";
		try {
			/* 119 */ for (char c : value.toCharArray()) {
				/* 120 */ temp = "0000" + Integer.toHexString(c);
				/* 121 */ temp = "\\u" + temp.substring(temp.length() - 4);
				/* 122 */ result = result + temp;
			}
		} catch (Exception e) {
			/* 125 */ e.printStackTrace();
		}
		/* 127 */ return result;
	}

	public static final boolean isEmpty(String s) {
		/* 137 */ return (s == null) || (s.trim().isEmpty());
	}

	public static final String getIp(HttpServletRequest request, HttpServletResponse response) {
		/* 149 */ String ip = request.getHeader("x-forwarded-for");
		/* 150 */ if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
			/* 151 */ ip = request.getHeader("Proxy-Client-IP");
		}
		/* 153 */ if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
			/* 154 */ ip = request.getHeader("WL-Proxy-Client-IP");
		}
		/* 156 */ if ((ip == null) || (ip.length() == 0) || ("unknown".equalsIgnoreCase(ip))) {
			/* 157 */ ip = request.getRemoteAddr();
		}
		/* 159 */ return ip;
	}

	public static final boolean isIPAddress(String s) {
		/* 165 */ if (isEmpty(s))/* 166 */ return false;
		/* 167 */ Pattern p = Pattern.compile(
				"(((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))[.](((2[0-4]\\d)|(25[0-5]))|(1\\d{2})|([1-9]\\d)|(\\d))");
		/* 168 */ Matcher m = p.matcher(s);
		/* 169 */ return m.matches();
	}

	public static String getMd5(String data) {
		/* 173 */ return DigestUtils.md5Hex(data);
	}

	public static final String digest(String src) {
		try {
			/* 184 */ MessageDigest digest = MessageDigest.getInstance("MD5");
			/* 185 */ digest.update(src.getBytes("UTF-8"));
			/* 186 */ return toHex(digest.digest());
		} catch (Exception e) {
		}
		/* 188 */ return null;
	}

	public static final String MD5(String s) {
		/* 199 */ String hexDigits = "0123456789abcdef";
		try {
			/* 201 */ byte[] strTemp = s.getBytes("gb2312");
			/* 202 */ MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			/* 203 */ mdTemp.update(strTemp);
			/* 204 */ byte[] raw = mdTemp.digest();
			/* 205 */ StringBuilder hex = new StringBuilder(2 * raw.length);
			/* 206 */ for (byte b : raw) {
				/* 207 */ hex.append(hexDigits.charAt((b & 0xF0) >> 4)).append(hexDigits.charAt(b & 0xF));
			}
			/* 209 */ return hex.toString();
		} catch (Exception e) {
		}
		/* 211 */ return null;
	}

	public static final String toHex(Serializable s) throws IOException {
		/* 216 */ return toHex(RawUtil.serialize(s));
	}

	public static final Object fromHex(String hex) throws IOException, ClassNotFoundException {
		/* 220 */ return RawUtil.deserialize(toBytes(hex));
	}

	public static String align(String value, int len, char c, boolean leftAlign) {
		/* 235 */ String result = "";
		/* 236 */ if (leftAlign) {
			/* 237 */ for (int i = 0; i < len; i++) {
				/* 238 */ if (i < value.length()) {
					/* 239 */ result = result + value.charAt(i);
				} else/* 241 */ result = result + c;
			}
		} else {
			/* 244 */ for (int i = 0; i < len; i++)
				/* 245 */ if (i < value.length()) {
					/* 246 */ result = result + value.charAt(i);
				} else/* 248 */ result = c + result;
		}
		/* 250 */ return result;
	}

	public static String trim(String src, int len, String triming) {
		/* 267 */ if ((src == null) || (len <= 0))/* 268 */ return "";
		/* 269 */ byte[] bStr = src.getBytes();
		/* 270 */ if (len >= bStr.length)/* 271 */ return src;
		/* 272 */ String cStr = new String(bStr, len - 1, 2);
		/* 273 */ if ((cStr.length() == 1) && (src.contains(cStr)))/* 274 */ len--;
		/* 275 */ return new String(bStr, 0, len) + triming;
	}

	public static String replace(String src, int start, int len, String replacement) {
		/* 279 */ if (src.length() < start + len - 1) {
			/* 280 */ return src;
		}
		/* 282 */ return src.substring(0, start - 1) + replacement + src.substring(start + len - 1);
	}

	public static String connect(String[] src, String c) {
		/* 296 */ if ((src == null) || (src.length == 0))/* 297 */ return "";
		/* 298 */ StringBuffer sb = new StringBuffer("");
		/* 299 */ sb.append(src[0]);
		/* 300 */ for (int i = 1; i < src.length; i++)
			/* 301 */ sb.append(c).append(src[i]);
		/* 302 */ return sb.toString();
	}

	public static boolean isEmail(String emailStr) {
		if (isEmpty(emailStr)) {
			return false;
		}
		/* 314 */ String regEmail = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
		/* 315 */ return match(emailStr, regEmail, true);
	}

	public static boolean isMobile(String mobileStr) {
		if (isEmpty(mobileStr)) {
			return false;
		}
		/* 328 */ String regMobile = "(13[0-9]|15[0-9]|17[0-9]|18[0-9]|14[0-9])\\d{8}";
		/* 329 */ return match(mobileStr, regMobile, false);
	}

	public static String getMobileCorp(String mobileStr) {
		/* 339 */ if (mobileStr.contains("+")) {
			/* 340 */ mobileStr = mobileStr.replace("+86", "");
		}

		/* 343 */ String telephone = mobileStr.substring(0, 3);
		/* 344 */ int telArea = Integer.valueOf(telephone).intValue();

		/* 346 */ int[] TELS_YD = { 134, 135, 136, 137, 138, 139, 147, 150, 151, 152, 154, 157, 158, 159, 187, 188 };
		/* 347 */ for (int i = 0; i < TELS_YD.length; i++) {
			/* 348 */ if (telArea == TELS_YD[i]) {
				/* 349 */ return "mobile";
			}
		}

		/* 353 */ int[] TELS_LT = { 130, 131, 132, 133, 155, 156, 185, 186 };
		/* 354 */ for (int i = 0; i < TELS_LT.length; i++) {
			/* 355 */ if (telArea == TELS_LT[i]) {
				/* 356 */ return "unicom";
			}
		}

		/* 360 */ int[] TELS_DX = { 189, 180 };
		/* 361 */ for (int i = 0; i < TELS_DX.length; i++) {
			/* 362 */ if (telArea == TELS_LT[i]) {
				/* 363 */ return "telecom";
			}
		}
		/* 366 */ return null;
	}

	public static boolean isIDNumber(String IDNumber) {
		/* 378 */ boolean result = IDNumber.matches("[0-9]{15}|[0-9]{17}[0-9X]");
		/* 379 */ if (result) {
			int date;
			int year;
			int month;
			//			int date;
			if (IDNumber.length() == 15) {
				/* 382 */ year = Integer.parseInt(IDNumber.substring(6, 8));
				/* 383 */month = Integer.parseInt(IDNumber.substring(8, 10));
				/* 384 */ date = Integer.parseInt(IDNumber.substring(10, 12));
			} else {
				/* 386 */ year = Integer.parseInt(IDNumber.substring(6, 10));
				/* 387 */ month = Integer.parseInt(IDNumber.substring(10, 12));
				/* 388 */ date = Integer.parseInt(IDNumber.substring(12, 14));
			}
			/* 390 */ switch (month) {
			case 2:
				/* 392 */ result = (date >= 1) && (year % 4 == 0 ? date <= 29 : date <= 28);
				/* 393 */ break;
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				/* 401 */ result = (date >= 1) && (date <= 31);
				/* 402 */ break;
			case 4:
			case 6:
			case 9:
			case 11:
				/* 407 */ result = (date >= 1) && (date <= 31);
				/* 408 */ break;
			default:
				/* 410 */ result = false;
			}

		}
		/* 414 */ return result;
	}

	public static boolean match(String src, String reg, boolean caseIgnore) {
		/* 431 */ Pattern pattern = caseIgnore ? Pattern.compile(reg, 2) : Pattern.compile(reg);
		/* 432 */ Matcher matcher = pattern.matcher(src);
		/* 433 */ return matcher.matches();
	}

	public static String preview(String src, int len, String str) {
		/* 450 */ if ((src != null) && (src.length() > len)) {
			/* 451 */ src = src.substring(0, len - 1) + str;
		}
		/* 453 */ return src;
	}

	public static final String get6Radom() {
		/* 462 */ String newString = null;

		/* 465 */ double doubleP = Math.random() * 1000000.0D;

		/* 468 */ if (doubleP >= 1000000.0D) {
			/* 469 */ doubleP = 999999.0D;
		}

		/* 473 */ int tempString = (int) Math.ceil(doubleP);

		/* 476 */ newString = String.valueOf(tempString);

		/* 479 */ while (newString.length() < 6) {
			/* 480 */ newString = "0" + newString;
		}

		/* 483 */ return newString;
	}

	/**
	 * @Title <p>校验用户名，用户名长度必须为1-225位，用户名只能是字母、数字、汉字的组合</p>
	 * @Description <p>校验用户名，用户名长度必须为1-225位，用户名只能是字母、数字、汉字的组合</p>
	 * @author wenxh
	 * @param username
	 * @return
	 * @return boolean 返回类型
	 * @date 2016年10月21日
	 * @throw
	 */
	public static boolean checkUserName(String username) {
		if (isEmpty(username)) {
			throw new RuntimeException("用户名不允许为空");
		}
		if (username.length() < 0 || username.length() > 225) {
			throw new RuntimeException("用户名长度必须为1-225位");
		}
		for (int nIndex = 0; nIndex < username.length(); nIndex++) {
			char cCheck = username.charAt(nIndex);
			if ((!isDigit(cCheck)) && (!isAlpha(cCheck)) && (!isChinese(cCheck))) {
				throw new RuntimeException("用户名只能是字母、数字、汉字的组合");
			}
		}
		return true;
	}

	/**
	 * @Title <p>校验密码是否合法,长度为6-18，必须包含数字和字母</p>
	 * @Description <p>校验密码是否合法,长度为6-18，必须包含数字和字母</p>
	 * @author wenxh
	 * @param username
	 * @return
	 * @return boolean 返回类型
	 * @date 2016年10月25日
	 * @throw
	 */
	public static boolean checkPassWd(String passwd) {
		if (isEmpty(passwd)) {
			throw new RuntimeException("密码不允许为空");
		}
		if (passwd.length() < 6 || passwd.length() > 18) {
			throw new RuntimeException("密码长度必须为6-18位");
		}
		int a = 0;
		int b = 0;
		for (int nIndex = 0; nIndex < passwd.length(); nIndex++) {
			char cCheck = passwd.charAt(nIndex);
			if (isAlpha(cCheck)) {
				a++;
			}
			if (isDigit(cCheck)) {
				b++;
			}
		}
		if (a == 0 || b == 0) {
			throw new RuntimeException("密码必须包含字母和数字");
		}
		return true;
	}

	public static boolean isDigit(char c) {
		/* 516 */ return ('0' <= c) && (c <= '9');
	}

	public static boolean isAlpha(char c) {
		/* 527 */ return (('a' <= c) && (c <= 'z')) || (('A' <= c) && (c <= 'Z'));
	}

	public static boolean isChinese(char c) {
		/* 539 */ Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);

		/* 541 */ if ((ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) ||

		/* 543 */ (ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS) ||

		/* 545 */ (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A) ||

		/* 547 */ (ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) ||

		/* 549 */ (ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION) ||

		/* 551 */ (ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS)) {
			/* 553 */ return true;
		}

		/* 557 */ return false;
	}

	public static boolean isC(String str) {
		boolean strflag = false;

		for (int i = 0; i < str.length(); i++) {
			strflag = String.valueOf(str.charAt(i)).matches("[一-?]");
		}
		return strflag;
	}

	public static Long toLong(String value) {
		/* 82 */ Long result = 0L;
		try {
			/* 84 */ result = Long.valueOf(value);
		} catch (Exception localException) {
		}

		/* 88 */ return result;
	}
}
